package com.cskaoyan.wordmemorize.controller.app;

import cn.dev33.satoken.stp.StpUtil;
import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.app.PayOrderDTO;
import com.cskaoyan.wordmemorize.dto.app.VocPaySaleDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.PaySuccessNotifyCommand;
import com.cskaoyan.wordmemorize.request.VocPaySaleCommand;
import com.cskaoyan.wordmemorize.service.UserPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;

/**
 * @author 顾海龙
 * @version 1.0
 */
@RestController
@ResponseBody
public class PayController {

    @Autowired
    UserPayService userPayService;

    //用户支付
    @PostMapping("/user/pay")
    public Result userPayment(@RequestBody VocPaySaleCommand command) {
        PayOrderDTO pay;
        try {
            pay = userPayService.pay(command, StpKit.USER.getLoginIdAsLong());
        } catch (NumberFormatException e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok(pay);
    }


    //用户用于接收支付成功的异步通知
    @PostMapping("/user/pay/notice")
    public Result paySuccessful(@RequestBody PaySuccessNotifyCommand command) {
        String s;
        try {
            s = userPayService.paySuccess(command, command.getUserId());

        } catch (NumberFormatException e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok(s);
    }


    //用户用于获取支付结果
    @GetMapping("/user/pay/{paySaleId}")
    public Result getPayResult(@PathVariable Long paySaleId) {
        Integer flag = 0;
        try {
            flag = userPayService.queryTradeStatus(paySaleId);
        } catch (Exception e) {
            return Result.ok("" + flag);
        }
        return Result.ok("" + flag);
    }


    //用户用于查看购买历史
    @GetMapping("/user/pay/list")
    public Result getOrderRecord(@ModelAttribute PageRequest pageRequest) {
        PageDTO<VocPaySaleDTO> paySaleHistory = userPayService.getPaySaleHistory(StpKit.USER.getLoginIdAsLong(), pageRequest);
        return Result.ok(paySaleHistory);
    }

}
